<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // let a = "a";
    var a = "a";
    console.log(this.a);

    function t1() {
        console.log(this.a);
        this.say = function () {
            console.log("say方法")
            console.log(this.a);
        }
    }

    t1();//this指向window
    let p = new t1();
    p.say();
    p.a = "haha";
    p.say();//this指向对象p

    let p1 = new t1();
    p1.say();

    let obj = {
        id: 1,
        name: "haha",
        say() {
            //方法调用属性和方法时，必须通过this.
            console.log(this.name);
            console.log(this.a);
        }
    }
    obj.a = "this is a";
    obj.say();//this指向对象obj

    this.name = "这是window中的name属性";
    function hello() {
        console.log(this.name);
    }

    obj.sayHello = hello;
    obj.sayHello();//this指向对象obj
    window.hello();//this指向window

    let windowSay = obj.say;
    windowSay();//this指向window
    console.log("------------------------------")
    let obj2 = {
        name: "这是obj2",
        say(callback) {
            console.log(this.name);
            callback();//this指向window
        },
        sayHello() {
            console.log("sayHello");
            console.log(this.name);
        }
    }

    function t3() {
        console.log("这是一个回调函数");
        console.log(this.name);
    }

    obj2.say(t3);//this指向obj2

    console.log("------------------------------")
    function t4(callback) {
        console.log("t4");
        console.log(this.name);
        callback();//this指向window
    }

    t4(obj2.sayHello);//this指向window

    console.log("------------------------------")
    let obj3 = {
        name: "这是obj3",
        sayHello() {
            console.log("sayHello");
            console.log(this.name);
        }
    }
    obj2.say(obj3.sayHello);//this指向对象obj2

    console.log("------------------------------")
    function t5(callback) {
        console.log("t5");
        callback();//this指向window
    }

    let objT5 = new t5(obj3.sayHello);

    console.log("------------------------------")
    function t6() {
        console.log(this.name);
        function t61() {
            console.log(this.name);
        }

        t61();
    }

    t6();

    console.log("------------------------------")
    let obj4 = {
        name: "这是obj3",
        sayHello() {
            console.log("sayHello");
            console.log(this.name);
            function t1() {
                console.log(this.name);
            }

            t1();//this指向window
        }
    }

    obj4.sayHello();//this指向对象obj4
</script>
</body>
</html>